﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_Contents_ContentEditor
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hidID.Value = Request.QueryString("Id")
            If hidID.Value <> "" Then
                Dim ArrId As String() = hidID.Value.Split(",")
                If (ArrId.Length <= 1) Then
                    LoadListStatus(hidID.Value)
                End If
            End If


            'active tab tuong ung voi Page
            'InVisble button ko can thiet
            If Request.PhysicalPath.Contains("List") Then
                lbtList.CssClass = "current_manage"
                lbtList.Text = "<span class=""current_manage"">Quản lý</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                cboAction.Visible = False
                lblStatus.Visible = False

            ElseIf Request.PhysicalPath.Contains("Create") Then
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                cboAction.Visible = False
                lblStatus.Visible = False
                'Permission
                'lbtAdd.Visible = HasPermission(Function_Name.Tin_Tuc, Session("RoleID"), 0, Audit_Type.Create)

            ElseIf Request.PhysicalPath.Contains("Detail") Then


                lbtView.CssClass = "current_manage"
                lbtView.Text = "<span class=""current_manage"">Xem</span>"
                'Permission
                setPermission()

            ElseIf Request.PhysicalPath.Contains("Edit") Then
                lbtEdit.CssClass = "current_manage"
                lbtEdit.Text = "<span class=""current_manage"">Sửa</span>"
                'Permission
                setPermission()
            End If
        End If
    End Sub
    Protected Sub setPermission()
        lbtView.Visible = HasPermission(Function_Name.Content, Session("RoleID"), hidSatus.Value, Audit_Type.ViewContent)
        lbtEdit.Visible = HasPermission(Function_Name.Content, Session("RoleID"), hidSatus.Value, Audit_Type.Edit)

        'set permisstion cho delete
        btnDelete.Visible = HasPermission(Function_Name.Content, Session("RoleID"), hidSatus.Value, Audit_Type.Delete)
        btnDelete.Attributes.Add("onclick", "return ComfirmDialog('Bạn có chắc chắn muốn xóa không?',0,'" + btnDelete.ClientID + "','" + hidSatus.Value + "',1);")

    End Sub
    'Load list action status of current status
    Sub LoadListStatus(ByVal iContentId As Integer)
        Using Data As New VSEntities
            'Get Current StatusId of object
            Dim cst = (From q In Data.Contents Where q.Id = iContentId Select q).SingleOrDefault
            Dim iCurrentStatusId As Byte = cst.StatusId

            hidType.Value = cst.TypeId
            hidSatus.Value = cst.StatusId
            hidID.Value = cst.Id


            'Load action tuong ung voi current statusid
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId Select q Order By
                    q.NextStatusId).ToList()

            For Each i In st
                If HasPermission(Function_Name.Content, Session("RoleID"), i.CurrentStatusId, i.Audit) Then
                    Dim _item As New ListItem(i.Title, i.NextStatusId)
                    cboAction.Items.Insert(0, _item)
                End If
            Next

            Dim iTem As New ListItem(cst.Status.Title, iCurrentStatusId)
            cboAction.Items.Insert(0, iTem)
        End Using
    End Sub
    Protected Sub cboAction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAction.SelectedIndexChanged
        'Nếu trạng thái đang chọn khác với trạng thái ban đầu thì mới đi chuyển trạng thái
        If (cboAction.SelectedValue <> cboAction.Items(0).Value) Then
            Response.Redirect("Action.aspx?Id=" & hidID.Value & "&CStatusId=" & cboAction.Items(0).Value & "&NStatusId=" & cboAction.SelectedValue)
        End If
    End Sub

    Protected Sub lbtList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtList.Click
        If Not Request.QueryString("Id") Is Nothing Then
            Using data As New VSEntities
                Dim intId As Integer = Request.QueryString("Id")
                Dim q = (From p In data.Contents Where p.Id = intId Select p).FirstOrDefault
                If q Is Nothing Then
                    Response.Redirect("List.aspx")
                Else

                    If Not q.MenuId Is Nothing Then
                        Dim MenuId As Integer = CInt(q.MenuId)
                        Response.Redirect("List.aspx?MenuId=" + MenuId.ToString)
                    Else
                        Response.Redirect("List.aspx")
                    End If


                End If

            End Using
        ElseIf Not Request.QueryString("MenuId") Is Nothing Then
            Response.Redirect("List.aspx?MenuId=" + Request.QueryString("MenuId"))
        Else
            Response.Redirect("List.aspx")
        End If
    End Sub

    Protected Sub lbtEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtEdit.Click
        If hidType.Value = 1 Then
            Response.Redirect("Edit_News.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 2 Then
            Response.Redirect("Edit_Video.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 3 Then
            Response.Redirect("Edit_Poll.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 4 Then
            Response.Redirect("Edit_Image.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 5 Then
            Response.Redirect("Edit_Tip.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")

        End If
    End Sub
    Protected Sub lbtView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtView.Click
        If hidType.Value = 1 Then
            Response.Redirect("Detail_News.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 2 Then
            Response.Redirect("Detail_Video.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 3 Then
            Response.Redirect("Detail_Poll.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 4 Then
            Response.Redirect("Detail_Image.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        ElseIf hidType.Value = 5 Then
            Response.Redirect("Detail_Tip.aspx?type=" & hidType.Value & "&Id=" & hidID.Value & "")
        End If

    End Sub
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New VSEntities
            intId = hidID.Value
            Dim q = (From p In data.Contents Where p.Id = intId Select p).FirstOrDefault
            strLogName = q.Title.ToString
            Dim MenuId As Integer = CInt(q.MenuId)
            Try
                data.Contents.DeleteObject(q)
                data.SaveChanges()
                Insert_App_Log("Delete Contents:" & strLogName & "", Function_Name.Content, Audit_Type.Delete, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))


                Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');", Me.Page, True)
                Response.Redirect("List.aspx?MenuId=" + MenuId.ToString)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
#End Region


End Class
